package com.helpustotravel.model.dao;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.helpustotravel.core.DAOBase;
import com.helpustotravel.model.jdo.Traveller;

public class TravellerDAO extends DAOBase {

	public TravellerDAO(PersistenceManager pm) {
		super(pm);
	}
	
	public Traveller addTraveller(Traveller traveller) {
		try {
			return pm.makePersistent(traveller);
		} finally {
			
		}
	}
	
	@SuppressWarnings("unchecked")
	public Traveller getTraveller(String name) {
		Query query = pm.newQuery("select from " + Traveller.class.getName() + 
								" where name == nameParam " + 
								"parameters String nameParam");
		List<Traveller> travellers = (List<Traveller>)query.execute(name);
		
		if(travellers.size() > 0) {
			Traveller traveller = travellers.get(0);
			
			return traveller;
		} else {
			return null;
		}	
	}
	
	@SuppressWarnings("unchecked")
	public List<Traveller> getAllTraveller() {
		Query query = pm.newQuery("select from " + Traveller.class.getName()
								+ " order by name");
		List<Traveller> travellers = (List<Traveller>)query.execute();
		
		return travellers;
	}
	
	@SuppressWarnings("rawtypes")
	public int getNumberOfTravellers() {
		Query query = pm.newQuery("select name from " + Traveller.class.getName());
		List results = (List) query.execute();
		int tableSize = results.size();
		return tableSize;
	}

}
